<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="../Templates/InnerSystemTemplate.xhtml"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core">
    <ui:define name="head-title">
        <title>Message Me | Merlion's ERP System</title>
    </ui:define>
    
    <ui:define name="content">
        <p:panel header="Inbox" id="panel">
            <f:view beforePhase="#{messageMeManagerBean.initView}">
                <h:form id="form" prependId="false">
                    <p:messages />
                    <p:dataTable var="message" value="#{messageMeManagerBean.messages}" dynamic="true" paginator="true" 
                                 rows="5" selectionMode="single" selection="#{messageMeManagerBean.selectedMessage}" onRowSelectUpdate="panel1 display1"
                                 emptyMessage="No message found in database." id="messagesTable"> 
                        <f:facet name="header">
                            <p:commandButton value="Create New Message" image="ui-icon ui-icon-pencil" oncomplete="createMessageDialog.show()" update="messagesTable" />
                            <p:commandButton value="Delete" image="ui-icon ui-icon-trash" oncomplete="deleteMessageDialog.show()" update="messagesTable" />
                        </f:facet>
                        <p:column sortBy="#{message.sender}" filterBy="#{message.sender}">
                        <f:facet name="header">
                            <h:outputText value="Sender"/>
                        </f:facet>
                            <h:outputText value="#{message.sender}"/>
                        </p:column>

                        <p:column sortBy="#{message.title}" filterBy="#{message.title}">
                        <f:facet name="header">
                            <h:outputText value="Message"/>
                        </f:facet>
                            <h:outputText value="#{message.title}"/>
                        </p:column>
                        

                    </p:dataTable>
                    <p:panel id="panel1" header="Message Display">
                        
                        <h:panelGrid id="display1" >
                            <h:panelGroup>
                                <h:outputLabel value="From: " style="font-weight: bold"/> 
                                <h:outputText value="#{messageMeManagerBean.selectedMessage.sender}" /> 
                            </h:panelGroup>
                            <br/>
                            <h:panelGroup>
                                <h:outputText value="Sent at: " style="font-weight: bold"/> 
                                <h:outputText value="#{messageMeManagerBean.selectedMessage.messageDate}" /><br/>
                            </h:panelGroup>
                            <br />
                            <h:panelGroup>
                                <h:outputText value="Title: " style="font-weight: bold"/> 
                                <h:outputText value="#{messageMeManagerBean.selectedMessage.title}" /><br/>
                            </h:panelGroup>
                            <br />
                            <h:panelGroup>
                                <h:outputLabel value="Message: " style="font-weight: bold" />
                                <h:outputText value="#{messageMeManagerBean.selectedMessage.message}"/> 
                            </h:panelGroup>
                            <p:separator/>
                            <h:panelGroup>
                                <h:outputText value="Title: " style="font-weight: bold"/> 
                                <p:inputText value="#{messageMeManagerBean.replyTitle}" /><br/>
                            </h:panelGroup>
                            <br/>
                            <h:outputLabel for="replyMessage" value="Reply: " style="font-weight: bold"/>
                            <p:inputTextarea id="replyMessage" value="#{messageMeManagerBean.replyMessage}" autoResize="false" style="width: 400px"/>

                            <p:commandButton value="Reply" image="ui-icon ui-icon-pencil" actionListener="#{messageMeManagerBean.reply(event)}" oncomplete="messageDisplayDialog.hide()" update="panel" ajax="true">                                     
                            </p:commandButton>

                            </h:panelGrid>
                    </p:panel>

                    <p:confirmDialog header="Delete Message" widgetVar="deleteMessageDialog" message="Do you want to delete this message?">
                        <p:commandButton value="Yes" update="form" oncomplete="deleteMessageDialog.hide()" actionListener="#{messageMeManagerBean.deleteMessage}" />
                          <p:commandButton value="No" update="messagesTable" oncomplete="deleteMessageDialog.hide()" />
                    </p:confirmDialog>
                    
                    <p:poll interval="5" listener="#{messageMeManagerBean.refresh(event)}" update="messagesTable"/>
                </h:form>
                
                <h:form>
                    <p:dialog widgetVar="createMessageDialog" resizable="false" width="600" height="300">
                        <h:panelGrid columns="2" cellpadding="4">
                            <h:panelGroup>
                                <h:outputLabel value="To: " style="font-weight: bold"/>                    
                                <p:autoComplete value="#{messageMeManagerBean.receiver}" required="true" label="Receiver" requiredMessage="Receiver is required." id="receiver" completeMethod="#{messageMeManagerBean.complete}" /> 
                            </h:panelGroup>
                            <br/><br/>
                            <h:panelGroup>
                                <h:outputLabel value="Title: " style="font-weight: bold"/> 
                                <p:inputText value="#{messageMeManagerBean.title}" /> 
                            </h:panelGroup>
                            <br/><br/>
                            <h:panelGroup>
                                <h:outputLabel for="sendMessage" value="Message: " style="font-weight: bold"/><br/>
                                <p:inputTextarea id="sendMessage" value="#{messageMeManagerBean.messageText}" autoResize="false" style="width: 400px"/>
                            </h:panelGroup>
                            <br/>
                            <p:commandButton value="Send" image="ui-icon ui-icon-pencil" actionListener="#{messageMeManagerBean.send(event)}" oncomplete="createMessageDialog.hide()" update="panel" ajax="true">                                     
                            </p:commandButton>
                        </h:panelGrid>
                    </p:dialog>
                </h:form>

                <h:form prependId ="false">      
                    
                </h:form>
                <h:form>
                    <p:dialog header ="New Message Notification" resizable="true" closable="true" widgetVar="newMessageDialog" >
                        <h:outputText value="You have new message."/>
                    </p:dialog>
                </h:form>
            </f:view>
        </p:panel>
    </ui:define>
    <ui:define name="left-content">

    </ui:define>    
</ui:composition>
